package com.rockcent.api.controller;

import com.rockcent.api.Routes;
import com.rockcent.api.helper.UserHelper;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.lib.domain.MallMember;
import com.rockcent.mall.lib.domain.MallMemberRechargeFlow;
import com.rockcent.mall.lib.domain.enumclass.MallMemberFundTransactionType;
import com.rockcent.mall.lib.dto.MallMemberDto;
import com.rockcent.mall.lib.dto.MallMemberFundFlowDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.service.MallMemberRechargeFlowSrv;
import com.rockcent.service.MallMemberSrv;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.EnumUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 平台会员
 * Created by zhangjh on 2017/3/10.
 */
@RestController
@RequestMapping(value = Routes.API_VERSION)
public class MallMemberController extends BaseController {
    private final Logger log = LoggerFactory.getLogger(MallMemberController.class);

    @Autowired
    private UserHelper userHelper;

    @Autowired
    private MallMemberSrv mallMemberSrv;

    @Autowired
    private MallMemberRechargeFlowSrv mallMemberRechargeFlowService;

    /**
     * @api {get} /v1/mall/member/info M 会员账户信息
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     *
     * @apiSuccess {String} mallMember_status 平台会员状态 NOT_MEMBER("未认证"), PROBATION_MEMBER("试用期会员"), AUDITING("开通审核中"), AUDIT_REFUSE("开通失败"), MEMBER("已开通")
     * @apiSuccess {String} mallMember_remark 会员账户备注
     * @apiSuccess {BigDecimal} mallMember_balance 余额
     * @apiSuccess {long} mallMember_probationaryPeriodDays 累计试用天数
     * @apiSuccess {String} rechargeFlow_auditStatus  审核状态 ALL("全部"),WAIT("待审核"),AUDITED("已审核"), PASS("通过"),REFUSE("不通过")
     * @apiSuccess {String} rechargeFlow_remark 审核不通过原因
     * @apiSuccess {BigDecimal} rechargeFlow_rechargeAmount 汇款金额
     * @apiSuccess {String} rechargeFlow_rechargeAccount 汇款账号
     * @apiSuccess {String} rechargeFlow_rechargeDate 汇款日期
     * @apiSuccess {String} rechargeFlow_rechargeVoucher 凭证
     *
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "mallMember": {
     *       "id": 1,
     *       "dateCreated": "2017-03-10 10:17:41",
     *       "lastUpdated": "2017-03-10 11:46:53",
     *       "version": 1,
     *       "mallId": 51,
     *       "balance": 0,
     *       "dateEnabled": "2017-03-10 10:18:55",
     *       "dateDisabled": "2017-04-10 10:18:55",
     *       "probationaryPeriodDays": 30,
     *       "status": "AUDITING",
     *       "remark": "无知无罪"
     *     },
     *     "rechargeFlow": {
     *       "id": 2,
     *       "dateCreated": "2017-03-10 11:46:54",
     *       "lastUpdated": "2017-03-10 11:46:54",
     *       "version": 0,
     *       "seqNo": "001489117673114",
     *       "mallId": 51,
     *       "mallMemberId": 1,
     *       "rechargeAmount": 10000,
     *       "rechargeAccount": "6228480402564890018",
     *       "rechargeDate": "2016-03-10",
     *       "rechargeVoucher": "http://wx.qlogo.cn/mmopen/UOCHvzUGAIV8FXCZV5LEuCyEFNLvYqlw6aicxXiasYKr6qmesZKTiatyujJO42jdfW877UJsmeVj0HwsJRibGIxWSvlAFSiaicAD5V/0",
     *       "auditStatus": "WAIT",
     *       "remark":"审核不通过原因"
     *     }
     *   }
     * }
     */
    @RequestMapping(value = Routes.MALL_MEMBER_INFO, method = RequestMethod.GET)
    public JsonResult info(HttpServletRequest request) {
        UserInfoDto userDto = userHelper.getSession(request);
        Long mallId = userDto.mall.getId();

        RestDoing doing = jsonResult -> {

            MallMember mallMember = mallMemberSrv.findTop1ByMallId(mallId);
            MallMemberRechargeFlow rechargeFlow = mallMemberRechargeFlowService.findTop1ByMallMemberIdByLastUpdatedDesc(mallMember.getId());
            Map<String, Object> map = new HashedMap();
            map.put("mallMember", mallMember);
            map.put("rechargeFlow", rechargeFlow);
            jsonResult.data = map;

        };
        return doing.go(request, log);
    }



    /**
     * @api {get} /v1/mall/member/page R 平台会员账户列表
     * @apiGroup mall_member
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiParam {String} mallId <code>可选参数</code> 平台ID
     * @apiParam {String} mallName <code>可选参数</code>  平台名称
     * @apiParam {String} memberStatus <code>可选参数</code>  平台会员状态
     *
     * @apiSuccess {long} id 会员账户状态
     * @apiSuccess {long} mallId 平台商ID
     * @apiSuccess {String} mallName 平台名称
     * @apiSuccess {String} companyName 公司名称
     * @apiSuccess {BigDecimal} balance 账户余额

     * @apiSuccessExample {json} Success-Response:
     * {
     *   "code": "1",
     *   "desc": "成功",
     *   "data": {
     *     "data": [
     *       {
     *         "id": 1,
     *         "dateCreated": "2017-03-10 10:17:41",
     *         "lastUpdated": "2017-03-10 14:02:49",
     *         "mallId": 51,
     *         "mallName": "券控",
     *         "companyName": "券控",
     *         "balance": 10000,
     *         "dateEnabled": "2017-03-10 10:18:55",
     *         "dateDisabled": "2017-04-10 10:18:55",
     *         "probationaryPeriodDays": 30,
     *         "remark": "无知无罪"
     *       }
     *     ],
     *     "totalCount": 1,
     *     "totalPage": 1,
     *     "pageNo": 0,
     *     "pageSize": 10,
     *     "url": "/mall/member/page",
     *     "prevUrl": "/mall/member/page?pageNo=0&pageSize=10",
     *     "nextUrl": "/mall/member/page?pageNo=0&pageSize=10"
     *   }
     * }
     */
    @RequestMapping(value = Routes.MALL_MEMBER_PAGE, method = RequestMethod.GET)
    public JsonResult pageAll(MallMemberDto dto, HttpServletRequest request) {
//        UserInfoDto userDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {

            PageResult<MallMemberDto> pageResult = mallMemberSrv.findMallMemberPage(dto, getPage(dto.getPageNo(), dto.getPageSize()));
            pageResult.setUrl(Routes.MALL_MEMBER_PAGE);

            jsonResult.data = pageResult;
        };
        return doing.go(request, log);
    }





}
